package LeetCode;

import com.company.Tree.Tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}15:16
 */
public class LeetCode102 {

    public class TreeNode {
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode(int x) { val = x; }
    }
    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList();

        List<List<Integer>> list1s = new ArrayList<>();
        if (root == null){
            return list1s;
        }
        ((LinkedList<TreeNode>) queue).add(root);
        while (!queue.isEmpty()){
            List<Integer> list = new ArrayList<>();
            for (int i = queue.size(); i > 0; i--) {//queue size就相当于每层的个数
                TreeNode treeNode = queue.poll();
                list.add(treeNode.val);
                if (treeNode.left!=null)
                    ((LinkedList<TreeNode>) queue).add(treeNode.left);
                if (treeNode.right!=null){
                    ((LinkedList<TreeNode>) queue).add(treeNode.right);
                }
            }
            list1s.add(list);
        }
        return list1s;
    }
}
